<template>
	<div class="addTable">
		<top-bar :title="title || item.name" open = "3" @closeAddTable="back">
			<div slot="rightItem"></div>
		</top-bar>
		<div>
			<div class="group" v-for="(value,key,index) in typeListObj" :key="index">
				<div class="group-title">{{key | changeKey}}</div>
				<div v-for="item in value">
					<type-data ref="types" v-show="item.isBigFormPk !== '是'" @eventList="eventList($event)" :isHide="isShowList" :types="item" @resetEvent="resetEvent($event)" :isReset="isResetList" :key="item.id"></type-data>
				</div>	
			</div>			
		</div>
		<div class="bnt">
			<button style="background-color: #5CCA99;color: #FFFFFF;" type="button" @click="submit">确定</button>
			<button style="background-color: #EEEEEE;color: #A19FA0;" type="button" @click="back">取消</button>
		</div>
		<div class="seat"></div>
	</div>
</template>

<script>
	import topBar from '@components/topBar.vue'
	import typeData from '@components/assembly.vue'
	import { ref } from 'vue';
	import { getField } from '@request/api.js'
	import { submit } from '@assets/js/submit.js'
	export default{
		data(){
			return{
				typeListObj: {},
				isShowList:[], //传入子元素显示隐藏数组
				isResetList:{},//传入子元素的刷新
				tableId:'',
			}
		},
		props:['item','upDate','title'],
		methods:{
			resetEvent(list){
				this.isResetList = list
			},
			back(){
				this.$emit('closeAddTable')
			},
			submit() {
				const arr = submit(this.$refs.types)	
				let obj = arr[1]
				// 获取主表临时id并入参
				if(this.$refs.types!==undefined){
					this.$refs.types.forEach(item=>{
						if(item.isBigFormPk !== ''&&item.isBigFormPk !== undefined){
							this.$set(obj,item.isBigFormPk,this.$store.state._tmpBigId)
						}
					})	
				}				
				if (arr[0] === true) {
					if(this.upDate.isEdit){
						this.$axios.post(`/api/update?_form=${this.upDate.fromId}&_num=${this.upDate.num}`, obj).then(res => {
							if (res.data.code === 0) {
								this.$toast.success('修改成功')
								let arr = [this.upDate.fromId,this.upDate.num,this.upDate.ename]
								this.$emit('updateTable',arr)
							}
						})
					}else{
						this.$axios.post(`/api/save?_form=${this.item.refTableFormBigId}`, obj).then(res => {
							if (res.data.code === 0) {
								this.$toast.success('提交成功')
								let arr = [this.item.refTableFormBigId,res.data.dataNum,this.item.ename]
								this.$emit('addSuessed',arr)
							}
						})
					}					
				}
			},
		},
		created() {
			if(this.upDate.isEdit){
				getField({
					'_form': this.upDate.fromId,
					'_num': this.upDate.num,
					'_type': 'update'
				}).then(res=>{
					if(res.code === 0){
						this.typeListObj = res.fieldMapLists
					}
				})
			}else{
				getField({
					'_form': this.item.refTableFormBigId,
					'_type': 'add'
				}).then(res=>{
					if(res.code === 0){
						this.typeListObj = res.fieldMapLists
					}
				})
			}				
		},
		components:{
			topBar,
			typeData
		},
		filters:{
			changeKey(key){
				if(key === '无'){
					return '业务信息'
				}else{
					return key
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.addTable{
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 99;
		background-color: #FFFFFF;
	}
	.group{
		margin-top: vw(12);
	}
	.group-title{
		font-size: vw(16);
		border-bottom: vw(1) solid $line-1;
		padding: vw(12) vw(12);
		background-color: #FFFFFF;
	}
	.bnt {
		display: flex;
		flex-direction: row-reverse;
		flex-wrap: wrap;
		align-items: center;
		position: fixed;
		bottom: 0;
		left: 0;
		height: vw(60);
		width: 100vw;
		background-color: $bg-1;
		z-index: 3;
	}	
	.bnt button {
		padding: vw(5) vw(32);
		font-size: vw(14);
		border: none;
		outline: none;
		margin-right: 16px;
	}	
	.seat {
		height: vw(60);
		width: 100vw;
		background-color: #FFFFFF;
	}
</style>
